package com.psychologyCommunity.Community.config;

import com.psychologyCommunity.Community.quartz.AlphaJob;
import com.psychologyCommunity.Community.quartz.PostScoreReflashJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

//配置==》数据库==》调用
@Configuration
public class QuartzConfig {

    //FactoryBean 可简化Bean的实例化过程，和以前的beanFactory没有关系
    //1.通过FactoryBean封装Bean的实例化过程
    //2.将FactoryBean 装配到Spring容器中
    //3.将FactoryBean注入给其他的Bean
    //4.该Bean得到的是FactoryBean所管理的 对象实例
//    @Bean
    public JobDetailFactoryBean alphaJobDetail(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(AlphaJob.class);
        //这个FactoryBean的名字 和他的组名 还有是否是长久的保存 、以及任务是不是可以恢复的
        jobDetailFactoryBean.setBeanName("alphaJob");
        jobDetailFactoryBean.setGroup("alphaJobGroup");
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }
    //配置Trigger（SimpleTriggerFactoryBean,ConTriggerFactoryBean)
    //传入的形参名是alphaJobDetail 会和上面名为alphaJobDetail 的Bean匹配 会优先和同名的Bean进行匹配
//    @Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail){
        SimpleTriggerFactoryBean simpleTriggerFactoryBean = new SimpleTriggerFactoryBean();
        simpleTriggerFactoryBean.setJobDetail(alphaJobDetail);
        simpleTriggerFactoryBean.setBeanName("alphaTrigger");
        simpleTriggerFactoryBean.setGroup("alphaTriggerGroup");
        simpleTriggerFactoryBean.setRepeatInterval(3000);//执行任务频率，隔多久执行一次任务
        simpleTriggerFactoryBean.setJobDataMap(new JobDataMap());//这个Trigger的底层需要存储这个Job的状态，给他定义一个 类型对象传
        return simpleTriggerFactoryBean;
    }

    //刷新帖子的分数的JobDetail
   // @Bean
    public JobDetailFactoryBean alphaPostScoreRefreshJobDetail() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(PostScoreReflashJob.class);
        //这个FactoryBean的名字 和他的组名 还有是否是长久的保存 、以及任务是不是可以恢复的
        jobDetailFactoryBean.setBeanName("alphaPostScoreRefreshJob");
        jobDetailFactoryBean.setGroup("alphaPostScoreRefreshJobGroup");
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    //刷新帖子分数的Trigger
  //  @Bean
    public SimpleTriggerFactoryBean alphaPostScoreRefreshTrigger(JobDetail alphaPostScoreRefreshJobDetail){
        SimpleTriggerFactoryBean simpleTriggerFactoryBean = new SimpleTriggerFactoryBean();
        simpleTriggerFactoryBean.setJobDetail(alphaPostScoreRefreshJobDetail);
        simpleTriggerFactoryBean.setBeanName("alphaPostScoreRefreshTrigger");
        simpleTriggerFactoryBean.setGroup("CommunityTriggerGroup");
        simpleTriggerFactoryBean.setRepeatInterval(1000*60*5);//执行任务频率，隔五分钟执行一次任务
        simpleTriggerFactoryBean.setJobDataMap(new JobDataMap());//这个Trigger的底层需要存储这个Job的状态，给他定义一个 类型对象传
        return simpleTriggerFactoryBean;
    }

}
